/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client;

import com.daikit.commons.shared.bean.DkBaseUser;
import com.daikit.commons.shared.bean.DkLanguage;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxt.client.propertyaccess.DkLanguagePropertyAccess;


/**
 * Application model class in which all models needed in an application scope should be stored.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkBaseApplicationModel
{

	private final DkLogger log = DkLogger.getLog(DkBaseApplicationModel.class);

	/**
	 * Milliseconds in one day.
	 */
	public static final long MILLISECONDS_IN_1_DAY = 86400000l;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CONSTRUCTOR
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Constructor. Nothing is done in it.
	 */
	public DkBaseApplicationModel()
	{
		super();
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// ATTRIBUTES
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	protected DkBaseUser previousUserLogged = null;
	protected DkBaseUser userLogged = null;
	protected boolean fullSize = false;
	protected final DkCacheEntity<DkLanguage> availableLanguages = new DkCacheEntity<DkLanguage>(
			DkLanguagePropertyAccess.INSTANCE.key());
	private DkLanguage currentLanguage;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return whether the application is currently displayed in full screen.
	 */
	public boolean isFullSize()
	{
		return fullSize;
	}

	/**
	 * @return currently logged user. Null if no user is connected.
	 */
	public DkBaseUser getUserLogged()
	{
		return userLogged;
	}

	/**
	 * @return the availableLanguages
	 */
	public DkCacheEntity<DkLanguage> getAvailableLanguages()
	{
		return availableLanguages;
	}

	/**
	 * @return the currentLanguage
	 */
	public DkLanguage getCurrentLanguage()
	{
		return currentLanguage;
	}

	/**
	 * @param currentLanguage
	 *           the currentLanguage to set
	 */
	public void setCurrentLanguage(final DkLanguage currentLanguage)
	{
		this.currentLanguage = currentLanguage;
	}

	/**
	 * Set the currently connected user in the model.
	 * 
	 * @param userLogged
	 *           the new currently connected user.
	 */
	public void setUserLogged(final DkBaseUser userLogged)
	{
		if (this.userLogged != null)
		{
			previousUserLogged = this.userLogged;
		}
		this.userLogged = userLogged;
	}

	/**
	 * Retrieve the previously logged user.
	 * 
	 * @return the previously logged user. Can be null if no one got connected yet, and after always not null.
	 */
	public DkBaseUser getPreviousUserLogged()
	{
		return previousUserLogged;
	}

	/**
	 * Change the full screen status of the application.
	 * 
	 * @param fullSize
	 *           a boolean indicating whether the application should be displayed in full screen or not.
	 */
	public void setFullSize(final boolean fullSize)
	{
		this.fullSize = fullSize;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Is someone connected ?
	 * 
	 * @return true if there is a user logged and its id is not null.
	 */
	public boolean isLogged()
	{
		return userLogged != null && userLogged.getId() != null;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// UTILS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Method to be called for debug purpose. Display all the model attributes.
	 */
	public void debugModel()
	{
		log.debug("===================== DEBUG MODEL [START] =====================");
		log.debug("-- MODEL DATA --");
		log.debug("fullSize = " + fullSize);
		log.debug("===================== DEBUG MODEL [ END ] =====================");
	}

}
